/**
 * 
 */
package com.thor.palmmall.rest.communicate;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.AUserGroupQueryParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.common.SUserGroupQueryParamConverter;
import com.thor.palmmall.rest.communicate.converter.ACommunicateConverter;
import com.thor.palmmall.rest.communicate.converter.SCommunicateConverter;
import com.thor.palmmall.rest.communicate.converter.SCommunicateQueryParamConverter;
import com.thor.palmmall.rest.communicate.converter.UCNConverter;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.communicate.SCommunicate;
import com.thor.palmmall.spi.communicate.SCommunicateService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class ACommunicateServiceImpl implements ACommunicateService {

  @Autowired
  private SCommunicateService communicateService;
  @Autowired
  private SBasicService basicService;

  @Override
  public String save(ACommunicate communicate, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(communicate, "communicate");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = communicateService.save(
        SCommunicateConverter.getInstance().convert(communicate),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("无法保存商户沟通记录:", resp.getMessage());
    }
  }

  @Override
  public QueryResult<ACommunicate> query(ACommunicateQueryParam queryParam)
      throws ThorServiceException {
    SResponse<SQueryResult<SCommunicate>> resp = communicateService
        .query(SCommunicateQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), ACommunicateConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询商户沟通记录：" + resp.getMessage());
    }
  }

  @Override
  public ACommunicate get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SCommunicate> resp = communicateService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess()) {
      return ACommunicateConverter.getInstance().convert(resp.getBody());
    } else {
      throw new ThorServiceException("无法找到指定ID的商户沟通记录：" + resp.getMessage());
    }
  }

  @Override
  public void delete(String id, long version, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    SResponse<Void> resp = communicateService.delete(id, version,
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess()) {
    } else {
      throw new ThorServiceException("无法删除指定ID的商户沟通记录：" + resp.getMessage());
    }
  }

  @Override
  public QueryResult<UCN> brands(AUserGroupQueryParam queryParam) throws ThorServiceException {
    SResponse<SQueryResult<UCN>> resp = basicService
        .brands(SUserGroupQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), UCNConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询指定项目下的品牌列表：" + resp.getMessage());
    }
  }

  @Override
  public QueryResult<UCN> positions(AUserGroupQueryParam queryParam) throws ThorServiceException {
    SResponse<SQueryResult<UCN>> resp = basicService
        .positions(SUserGroupQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess()) {
      return SQueryResultConverter.convert(resp.getBody(), UCNConverter.getInstance());
    } else {
      throw new ThorServiceException("无法查询指定项目下的铺位列表：" + resp.getMessage());
    }
  }

}
